"""
Created on 2019年04月03日
@author:
"""

import requests
import unittest
import get_token
from get_token import get_tokenclass
import json
import re
import hashlib
import random


class Test(unittest.TestCase):

    def setUp(self):
        token_class = get_tokenclass()
        self.apiToken = token_class.token_creat()
        self.sign = token_class.signature_create()
        self.timeSTAP = get_token.time_stamp
        self.appKey = get_token.appKey
        self.appSecret = get_token.appSecret
        self.base_url = "http://10.10.10.100:8080"
        self.headers = {"Content-type": "application/json", "Connection": "close"}
        self.requestId = str(random.randint(0, 99999))  # 每次请求（每个case）生成不同的随机requestId

    def tearDown(self):
        pass

    # 删除酒店
    def test_001(self):
        params = {
            "header": {
                "appKey": self.appKey,
                "apiToken": self.apiToken,
                "requestId": self.requestId,
                "timestamp": self.timeSTAP,
                "sign": self.sign
            },
            "body": {
                "hotels": [
                    "aaa",
                    "bbb"
                ]
            }
        }
        body_list1 = str(params["body"])
        body_list2 = body_list1.replace(body_list1[25:32], "udid")
        body_list3 = re.sub("[[]", "", body_list2)
        body_list = re.sub("[]]", "", body_list3)
        list_sig = self.timeSTAP + self.apiToken + self.appSecret + self.appKey + body_list
        signature = hashlib.sha1(list_sig.encode('utf-8')).hexdigest().upper().strip()
        params["header"]["sign"] = signature
        res = requests.post(url=self.base_url + "/partner/hotel/remove", data=json.dumps(params),
                            headers=self.headers)  # 第二次签名验证
        response = json.loads(res.content.decode('utf-8'))
        self.assertEqual(response["msg"], "SUCCESS")


if __name__ == '__main__':
    mySuit = unittest.TestSuite()
tesTCases = ["test_001", "test_002", "test_003", "test_004", "test_005", "test_006", "test_007"]
for cs in tesTCases:
    mySuit.addTest(Test(cs))
# mySuit.addTest(Test("test_003"))
myRun = unittest.TextTestRunner()
myRun.run(mySuit)